<template>
	<view>
		<view class="recharge_box card_box">
			<view class="recharge_hint">
				充值金额
			</view>
			<view class="recharge_input">
				￥<input class="input" type="digit" value="" v-model="amount" />
			</view>
		</view>
		<view class="recharge_way card_box">
			<view class="recharge_hint">
				充值方式
			</view>
			<view class="recharge_way_list">
				<view class="recharge_way_item">
					<view class="menu_item" v-for="(item, i) in payType" :key="i" @click="showBankList(item)">
						<label class="radio">
							<view class="avatoa">
								<image :src="item.payImg" class="imgava"></image>
								<text class="menu_title">{{item.type === 'Bank'?bankName:item.payName}}</text>
							</view>
							<view class="">
								<text v-if="item.type === 'Bank'" class="menu_title arrow_icon">〉</text>
								<radio v-else color="#195ED7" value="" />
							</view>
						</label>
					</view>
				</view>
			</view>
		</view>
		<view class="boxsss" v-if="showBox">
			<view class="modal_boxs" style="position: relative;">
				<view style="padding: 40rpx 20rpx;display: flex;justify-content: center;align-items: center;">
					<text class="modal_box">请输入手机验证码</text>
				</view>
				
				<view class="uni-list-cell-db">
					<input type="text" v-model="yzm" style="border: 0.1px solid #ddd;" class="uni-input" placeholder="请输入验证码" />
				</view>
				<view style="display: flex;flex-direction: row; justify-content: center;align-items: center;">
					<button type="primary" class="lisis" style="background-color: #477EDA;margin: 30rpx;font-size: 28rpx;" @click="cancelFlag">取消</button>
					<button type="primary" class="lisis" style="background-color: #477EDA;margin: 30rpx;font-size: 28rpx;" @click="querenCK">确认</button>
				</view>
			</view>
		</view>
		<view style="display: flex;justify-content: center;align-items: center;">
			<button type="primary" class="btn_pri" style="background-color: #477EDA;" @click="stopConsume">确认充值</button>
		</view>
		<Popup ref="showBankList" title="请选择银行" :full="true">
			<view class="bankList">
				<radio-group @change="changeBank">
					<view class="menu_item bottom_line" v-for="(bank,index) in bankCardLists" :key="index">
						<label class="radio">
							<view class="avatoa">
								<image src="../../../static/images/card.png" class="imgava"></image>
								<view class="">
									<view class="">
										<text class="menu_title">{{bank.bankName}}</text>
									</view>
									<view class="">
										<text class="bank_card_no">{{bank.bankCardNo}}</text>
									</view>
								</view>
							</view>
							<view class="">
								<radio :value="bank.bankCardNo" :checked="index === current" color="#195ED7" />
							</view>
						</label>
					</view>
				</radio-group>
			</view>
		</Popup>
	</view>
</template>

<script>
	import {miniStudentOpenId} from '../../../common/api/index.js';
	import Popup from '@/components/popup/popup.vue'
	export default {
		data() {
			return {
				amount: '',
				showBox: false,
				yzm: '',
				bizOrderNo: '',
				payType: [{
						payName: '微信',
						payImg: '../../../static/images/weixin.png',
						path: './rechargeAmount',
						type: 'WeChat'
					},
					{
						payName: '支付宝',
						payImg: '../../../static/images/alipay.png',
						path: './rechargeAmount',
						type: 'Alipay'
					},
					{
						payName: '银行卡',
						payImg: '../../../static/images/card.png',
						path: './rechargeAmount',
						type: 'Bank'
					}
				],
				bankList: [],
				current: 0,
				bankChange: {},
				bankName: '银行卡充值',
				bankCardLists: []
			};
		},
		components: {
			Popup
		},
		onLoad() {
			// this.bankChange = this.bankList[this.current]
			// data: {
			// 	bizUserId: bizUserId,
			// 	accountSetNo: '400193',
			// 	amount: _that.amount,
			// 	payMode: 7,
			// 	wxMinOpenVO: {
			// 		wxAppId: 'wx4015a847131516ad',
			// 		wxMchtId: '1407873802',
			// 		acct: ress.data.openId,
			// 		cusip: '192.168.1.1'
			// 	}
			// },
		},
		onShow:function(){
			this._queryBankCard()
		},
		methods: {
			_queryBankCard() {
				uni.showLoading()
				let bizUserId = uni.getStorageSync('bizUserId')
				uni.request({
					method: 'POST',
					url: 'https://wlreaohs.xiaomy.net/common/biz/cloud/member/queryBankCard',
					data: {
						bizUserId: bizUserId
					},
					header: { 'content-type': 'application/json' },
					success: (res) => {
						if(res.data.code == 0) {
							if(res.data.data.bindCardList == null) {
								this.bankCardLists = []
							} else {
								this.bankCardLists = res.data.data.bindCardList
								this.bankChange = this.bankCardLists[this.current]
							}
							uni.hideLoading()
						} else {
							uni.hideLoading()
						}
					},
					fail: (err) => {
						uni.hideLoading()
						console.log(err)
					}
				})
			},
			cancelFlag() {
				this.showBox = false;
			},
			querenCK() {
				this.showBox = false
				let _thats = this
				let bizUserId = uni.getStorageSync('bizUserId')
				uni.showLoading({
					title: '确认支付中...'
				})
				uni.request({
					method: 'POST',
					url: 'https://qiancheng.mynatapp.cc/common/biz/cloud/order/payByBackSMS',
					data: {
						bizUserId: bizUserId,
						bizOrderNo: _thats.bizOrderNo,
						verificationCode: _thats.yzm
					},
					header: { 'content-type': 'application/json' },
					success: (ress) => {
						if(ress.data.code == 10000) {
							if(ress.data.data == null) {
								uni.hideLoading()
								uni.showModal({
									title: '温馨提示',
									content: `${ress.data.subMsg}`,
									showCancel: false
								});
							} else {
								uni.hideLoading()
								uni.showModal({
									title: '温馨提示',
									content: `充值成功！`,
									showCancel: false,
									success: function (res) {
									    if (res.confirm) {
											_thats.showBox = false
											uni.switchTab({
												url: '/pages/me/me'
											})
									    } else if (res.cancel) {
									        console.log('用户点击取消');
									    }
									}
								});
							}
						} else {
							uni.hideLoading()
							uni.showModal({
								title: '温馨提示',
								content: `${ress.data.msg}`,
								showCancel: false
							});
						}
					},
					fail: (err) => {
						uni.hideLoading()
						console.log(err)
					}
				})
			},
			stopConsume() {
				if(this.amount == '') {
					uni.showToast({
						title: '请输入充值金额',
						icon: 'none'
					})
				} else {
					let _that = this;
					uni.login({
						provider: 'weixin',
						success:function(response){
							if(response.errMsg == "login:ok") {
								miniStudentOpenId({
									code: response.code
								}).then(ress => {
									if(ress.code == 0) {
										let bizUserId = uni.getStorageSync('bizUserId')
										uni.showLoading({
											title: '支付中...'
										})
										uni.request({
											method: 'POST',
											url: 'https://qiancheng.mynatapp.cc/common/biz/cloud/order/depositApply',
											data: {
												accountSetNo: "400193",
												amount: _that.amount,
												bankCardNo: _that.bankChange.bankCardNo,
												bizUserId: bizUserId,
												goodsName: "sjw",
												validateType: 1,
												payMode: 3
											},
											header: { 'content-type': 'application/json' },
											success: (res) => {
												if(res.data.code == 10000) {
													uni.hideLoading()
													if(res.data.data == null) {
														uni.showModal({
															title: '提示',
															content: `${res.data.subMsg}`,
															showCancel: false
														});
													} else {
														uni.hideLoading()
														_that.showBox = true;
														_that.bizOrderNo = res.data.data.bizOrderNo;
													}
												} else {
													uni.hideLoading()
													uni.showModal({
														title: '提示',
														content: `${res.data.msg}`,
														showCancel: false
													});
												}
											},
											fail: (err) => {
												uni.hideLoading()
												console.log(err)
											}
										})
									}
								}).catch(err => {
									console.log(err)
								})
							} else {
								
							}
						},
					})
				}
			},
			showBankList(bank) {
				if (bank.type === 'Bank') this.$nextTick(() => {
					this.$refs.showBankList.open()
				})
			},
			changeBank(e) {
				this.bankChange = this.bankCardLists.filter((item,index) => {
					if(item.bankCardNo == e.target.value){
						this.current = index;
					}
					return item.bankCardNo == e.target.value;
				})[0]
				console.log(this.bankChange)
				this.$nextTick(() => {
					this.$refs.showBankList.close()
				})
			},
			withdrawAll() {
				this.amount = this.balance
			},
		},
	}
</script>
<style>
	page {
		background-color: #f8f8f8;
	}
</style>
<style lang="scss">
	.card_box {
		box-shadow: 0px 2px 8px 0px #ededed;
		margin-bottom: 20rpx;
		background-color: #FFF;
	}
	.btn_pri {
		border-radius: 30rpx;
		margin-top: 60rpx;
		height: 70rpx;
		line-height: 70rpx;
		text-align: center;
		width: 90%;
		font-size: 26rpx;
		color: #FFFFFF;
		box-shadow: rgba(25, 94, 215, 0.4) 0px 4px 6px 1px;
	}

	.recharge_hint {
		font-size: 34rpx;
		padding: 36rpx 36rpx 0;
	}

	.recharge_input {
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		align-items: center;
		font-size: 60rpx;
		padding: 36rpx;

		.input {
			width: 100%;
			height: 90rpx;
			line-height: 90rpx;
			padding: 24rpx;
			font-size: 80rpx;
		}
	}

	.recharge_way_list {
		margin-top: 24rpx;
	}
	.lisis {
		border-radius: 10rpx;
		width: 150rpx;
		height: 50rpx;
		text-align: center;
		line-height: 50rpx;
		color: #FFFFFF;
		box-shadow: rgba(25, 94, 215, 0.4) 0px 4px 6px 1px;font-size: 20rpx;
	}
	.menu_item {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		padding: 24rpx;
		background-color: #FFFFFF;

		.radio {
			width: 100%;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
		}

		&:active {
			background-color: #f6f6f6;
		}
	}


	.avatoa {
		display: flex;
		align-items: center;
	}

	.imgava {
		width: 56rpx;
		height: 56rpx;
		margin-right: 25rpx;
	}
	.boxsss {
		width: calc(100% - 40rpx);
		height: 100%;
		display: block;
		position: fixed;
		top: 0rpx;
		left: 0rpx;
		background-color: rgba(102, 102, 102, 0.498039215686275);
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 0 20rpx;
		z-index: 999;
	}
	.modal_boxs {
		width: 60%;
		display: flex;
		flex-direction: column;
		background-color: rgba(252, 252, 252, 1);
		border-radius: 10rpx;
		padding: 0 20px;
	}
	.menu_title {
		font-family: PingFangSC;
		font-weight: 400;
		font-size: 34rpx;
		color: #000000;
	}

	.arrow_icon {
		color: #333;
	}

	// 按钮组
	.submit {
		text-align: center;
		padding: 24upx;

		.submitBtn {
			border-width: 0px;
			height: 100upx;
			line-height: 100upx;
			background: inherit;
			background-color: '#477EDA';
			box-sizing: border-box;
			border-width: 0;
			border-radius: 6upx;
			-moz-box-shadow: none;
			-webkit-box-shadow: none;
			box-shadow: none;
			color: #FFFFFF;

			&.radius_btn {
				border-radius: 50upx;
				box-shadow: 0px 8px 9px 0px #c3defc;
			}

			&:active {
				background-color: #3266a5;
			}

			&::after {
				border: none;
			}
		}
	}
</style>
